//
//  ReleaseForumViewController.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/31.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "ReleaseForumViewController.h"
#import "ForumViewModel.h"

@interface ReleaseForumViewController ()<UITextViewDelegate,UITextFieldDelegate>
{
    UITextField* title;
    UITextView* discussText;
    UIButton* sure;
    UILabel* PlaceHolderLable;
    
}
@property (nonatomic,strong) ForumViewModel * forumViewModel;

@end

@implementation ReleaseForumViewController


-(ForumViewModel *)forumViewModel{
    
    if (!_forumViewModel) {
        _forumViewModel = [[ForumViewModel alloc] init];
    }
    return _forumViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    // Do any additional setup after loading the view.
}


-(void)initNav{
    
    [self.navigationItem setTitle:@"发布论坛"];
    [self.view setBackgroundColor:baseWhiteColor];
}
-(void)initUI{
    
    title=[UITextField new];
//    title.borderStyle = UITextBorderStyleRoundedRect;
    title.backgroundColor = baseWhiteColor;
    title.placeholder = @"取个好的标题是一个好帖的开始~";
    [title setValue:baseLightBlackColor forKeyPath:@"_placeholderLabel.textColor"];
    title.font =DEF_MyFont(16);
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = baseBlackColor;
    title.delegate = self;
    [self.view addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.view).with.offset(20);
        make.leading.equalTo(self.view).with.offset(15);
    }];
    
    UIView* line=[UIView new];
    line.backgroundColor=baseLineColor;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
        make.height.mas_equalTo(1);
        make.top.equalTo(title.mas_bottom).with.offset(2);
        make.leading.equalTo(self.view).with.offset(15);
    }];
    
    discussText=[UITextView new];
    discussText.layer.cornerRadius = 5;
    discussText.layer.masksToBounds = YES;
    discussText.layer.borderWidth=1;
    discussText.layer.borderColor=baseLineColor.CGColor;
    discussText.textColor = baseBlackColor;
    discussText.font = DEF_MyFont(16);
    discussText.delegate=self;
    discussText.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
    discussText.backgroundColor= baseWhiteColor;
    [self.view addSubview:discussText];
    [discussText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
        make.height.mas_equalTo(180);
        make.top.equalTo(line.mas_bottom).with.offset(20);
        make.leading.equalTo(self.view).with.offset(15);
    }];
    
    PlaceHolderLable = [[UILabel alloc]initWithFrame:CGRectMake(19, 15, 230, 20)];
    PlaceHolderLable.enabled = NO;
    PlaceHolderLable.text = @"写点什么和网友一起互动吧~";
    PlaceHolderLable.font = DEF_MyFont(16);
    PlaceHolderLable.textColor = baseLightBlackColor;
    [discussText addSubview:PlaceHolderLable];
    
    //    登录
    sure=[UIButton new];
    [sure setTitle:@"发布" forState:UIControlStateNormal];
    [sure setTitleColor:baseWhiteColor forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    sure.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    sure.backgroundColor=DEF_COLOR_MAINCOLOR;
    sure.layer.cornerRadius = 5.f;
    [self.view addSubview:sure];
    [sure mas_makeConstraints:^(MASConstraintMaker * make){
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-80);
        make.height.mas_equalTo(40);
        make.top.equalTo(discussText.mas_bottom).with.offset(40);
        make.leading.equalTo(self.view).with.offset(40);
    }];
    
}

-(void)sure:(UIButton* )sender{

    if (UserInfoShare.isLogin==YES) {
        
        if ([title.text isEqualToString:@""]) {
            
            [MXUtility showTips:@"标题不能为空哦~"];
            
        }else if ([discussText.text isEqualToString:@""]){
            
            [MXUtility showTips:@"论坛内容不能为空哦~"];
            
        }else{
            
            [MXUtility showMBProgress:self.view message:LoadingText];
            
            [[self.forumViewModel saveUserForumRequestParameter:UserInfoShare.userCenterModel.userID title:title.text content:discussText.text] subscribeNext:^(id result) {
                
                if([result[@"errorcode"] integerValue] == 0){
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"发布成功" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *Sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        
                        if (self.backBlock) {
                            self.backBlock();
                        }
                    }];
                    
                    [alert addAction:Sure];
                    [self presentViewController:alert animated:YES completion:nil];
                    
                }
                [MXUtility hideMBProgress:self.view];
            }error:^(NSError *error) {
                
                [MXUtility hideMBProgress:self.view];
                
            }];

        }

    }else{
    
     [MXUtility showTips:@"请先前去登录"];
        
    }
    
}

-(void)textViewDidChange:(UITextView *)textView{
    
    if ([discussText.text length] == 0) {
        
        [PlaceHolderLable setHidden:NO];
        
    }else{
        
        [PlaceHolderLable setHidden:YES];
        
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [discussText resignFirstResponder];
    [title resignFirstResponder];

}

@end
